package editor.view;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;


import map.MapObject;

public class SidePanel extends JPanel implements InfoContainer{
	
	JLabel infocontainer;
	
	ArrayList<Component> propertieComponents;
	
	HashMap<JTextField, String> currentProperties;
	
	public SidePanel(){
		super();
		//setBackground(Color.LIGHT_GRAY);
		setPreferredSize(new Dimension(175,100));
		//setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		setLayout(new FlowLayout());
		infocontainer = new JLabel("", SwingConstants.LEFT);
		infocontainer.setSize(0, 50);
		add(infocontainer);
		
		propertieComponents = new ArrayList<Component>();
		currentProperties = new HashMap<JTextField, String>();
	}

	@Override
	public void print(MapObject arg) {
		if(arg != null){
			StringBuilder str = new StringBuilder();
			str.append(arg.toString());
			infocontainer.setText(wrapHTML(str.toString()));
		}

	}
	
	public void printProperties(){
		
	}

	private String wrapHTML(String text){
		return "<html><head></head><body>"+ text +"</body></html>";
	}

	public void printDetails(final MapObject arg) {
		for(Component comp : propertieComponents){
			remove(comp);
		}
		propertieComponents.clear();
		currentProperties.clear();
		
		
		JLabel className = new JLabel(arg.toString());
		propertieComponents.add(className);
		add(className);

		
		for(Entry entry : arg.getProperties().entrySet()){
			JPanel row = new JPanel();
			//row.setBorder(BorderFactory.createLoweredBevelBorder());
			row.setLayout(new FlowLayout(FlowLayout.LEFT));
			
			JLabel propertyName = new JLabel();
			propertyName.setText(entry.getKey().toString());
			row.add(propertyName);
			
			JTextField propertyValue = new JTextField();
			propertyValue.setColumns(10);
			propertyValue.setText(entry.getValue().toString());
		
			row.add(propertyValue);
			
			currentProperties.put(propertyValue, (String)entry.getKey());
			
			propertieComponents.add(row);
			add(row);
		}

		if(propertieComponents.size() > 1){
			JButton savePropertieChange = new JButton("save");
			savePropertieChange.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					HashMap<String, String> newProperties = new HashMap<String, String>();
					for(Entry entry : currentProperties.entrySet()){
						JTextField source = (JTextField)entry.getKey();
						newProperties.put((String)entry.getValue(), source.getText());
						
					}
					arg.setProperties(newProperties);
				}
			});
			propertieComponents.add(savePropertieChange);
			add(savePropertieChange);
		}
		
	}
}
